package com.song.Inet.Socket2;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Author ZhangGuisong
 * @Date 2021/5/4-12:02
 */
public class SocketServer {
    private BufferedOutputStream bufferedOutputStream;

    /**
     *  client 向server 发送图片
     *  server 接收图片，并保存到src 并向client 回应接收成功
     *  client 收到server的回应
     *
     */
    public static void main(String[] args) throws Exception {
        ServerSocket socket=new ServerSocket(1996);
        Socket socketServer = socket.accept();
        //接收资源
        InputStream inputStream = socketServer.getInputStream();
//        BufferedInputStream bufferedInputStream=new BufferedInputStream(inputStream);
//        bufferedInputStream.
          byte[] bytes = StreamUtils.streamToByteArray(inputStream);
          BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("src/zhang.jpg"));
          bufferedOutputStream.write(bytes);
          //给client 回复
        OutputStream outputStream = socketServer.getOutputStream();
        BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(outputStream));
        bufferedWriter.write("Server had accept u source");
        bufferedWriter.flush();
        socketServer.shutdownOutput();
        bufferedWriter.close();
        bufferedOutputStream.close();
        inputStream.close();
        socketServer.close();
    }
}
